# -*- coding: UTF-8 -*-


# 1 - 导入pygame
import pygame
from random import randint
import tkMessageBox
from pygame.locals import *

# 2 - 初始化游戏窗口
pygame.init()
width, height = 480, 480
screen = pygame.display.set_mode((width, height))  
black, red, blue, white = (0,0,0), (255,0,0), (0,0,255), (255,255,255)
empty = 0
xxx = -1
ooo = 1

# 3 - 画井字棋框线
def draw_game():
          pygame.draw.rect(screen, white, (0, 0, width, height))

          pygame.draw.line(screen, black, (160, 0), (160, 480), 5)
          pygame.draw.line(screen, black, (320, 0), (320, 480), 5)
          pygame.draw.line(screen, black, (0, 160), (480, 160), 5)
          pygame.draw.line(screen, black, (0, 320), (480, 320), 5)

          for row, line in enumerate(state):                          #row, line => 0,[0,0,0]  1,[0,0,0]  2,[0,0,0]
                    for col, val in enumerate(line):                  #col, val => 0,0  1,0  2,0
                              if val == xxx:
                                        # 画叉
                
                                        upper_left = (col * 160 + 5, row * 160 + 5)
                                        lower_right = (col * 160 + 155, row * 160 + 155)
                                        pygame.draw.line(screen, red, upper_left, lower_right, 5)
                
                                        upper_right = (col * 160 + 155, row * 160 + 5)
                                        lower_left = (col * 160 + 5, row * 160 + 155)
                                        pygame.draw.line(screen, red, upper_right, lower_left, 5)
                              elif val == ooo:
                                        #画圆
                                        rect = (col * 160 + 5, row * 160 + 5, 150, 150)
                                        pygame.draw.ellipse(screen, blue, rect, 5)
          pygame.display.flip()
def draw_O():
          while True:
                    row = randint(0,2)#随机0~2中数字
                    col = randint(0,2)
                    if state[row][col] == 0:
                              state[row][col] = 1
                              break
          draw_game()
          pygame.display.flip()

def is_won():
          for val in range(3):
                    # Check matching row. 检查匹配的行。

                    if state[0][val] == state[1][val] == state[2][val] != empty:    #判断每一列中三个图形是否都相同 (!= empty) 可以省略 --- 暂时<个人认为>
                              return state[0][val]
                              
                    # Check matching column. 检查匹配的列。

                    if state[val][0] == state[val][1] == state[val][2] != empty:    #判断每一行中三个图形是否都相同 (!= empty) 可以省略 --- 暂时<个人认为>
                              return state[val][0]

          # Check matching diagonal. 检查匹配的对角。

          if state[0][0] == state[1][1] == state[2][2] != empty:              #判断 \ 中三个图形是否都相同 (!= empty) 可以省略 --- 暂时<个人认为>
                    return state[1][1]

          if state[0][2] == state[1][1] == state[2][0] != empty:              #判断 / 中三个图形是否都相同 (!= empty) 可以省略 --- 暂时<个人认为>
                    return state[1][1]

def logout():
          pygame.quit()
          exit(0)

def begin():
          global state
          state = [[empty] * 3,[empty] * 3,[empty] * 3]
          draw_game()
          pygame.display.flip()
begin()

while True:
          pygame.display.flip()
          
          event = pygame.event.wait()

          pos = None

          temp = 0

          if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
          elif event.type == KEYDOWN:
                    if event.key == K_r:  #按R清空列表 相当于重新开始
                        
                        state=[[0,0,0] ,[0,0,0],[0,0,0]]
                        draw_game()
            
                    elif event.key == K_q: #按q退出游戏
                        
                        pygame.event.post(pygame.event.Event(QUIT))
          elif event.type == MOUSEBUTTONDOWN and event.button == 1: #用左键点击时获取鼠标的坐标值
                    pos = (event.pos[1] / 160, event.pos[0] / 160)
                    row, col = pos
                    state[row][col] = -1
                    draw_game()
                    for i in range(3):   #检查列表中等于0的数量
                              for j in range(3):
                                        if state[i][j] == 0:
                                                  temp += 1
                    if temp >= 2: #列表中等于0的数量大于等于2是 运行draw_O函数
                              draw_O()
          if is_won() == -1:
                    tkMessageBox.showinfo(title='say hello',message='You win') #is_won 返回值是-1 时 显示你赢
                    logout()
          elif is_won() == 1:
                    tkMessageBox.showinfo(title='say hello',message='You lose')#is_won 返回值是1 时 显示你输
                    logout()
